package com.cash.channel.svc.service.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.enums.BankCardBindStatusEnum;
import com.cash.channel.api.model.request.BankCardBindCaptchaRequest;
import com.cash.channel.api.model.response.BankCardBindCaptchaResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.channel.api.util.ValidatorUtil;
import com.cash.channel.svc.logic.BankCardLogic;
import com.cash.channel.svc.logic.ProtocolLogic;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.logic.UserLogic;
import com.cash.channel.svc.service.base.BankCardBindCaptcha;
import com.cash.user.model.UserInfoModel;
import com.google.common.collect.Maps;
import geex.payment.model.pojo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.绑卡验证码)
public class CommonBankCardBindCaptcha implements BankCardBindCaptcha {

    @Autowired
    private UserLogic userLogic;

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Autowired
    private BankCardLogic bankCardLogic;

    @Autowired
    private ProtocolLogic protocolLogic;

    @Override
    public ApiResponseModel<BankCardBindCaptchaResponse> doBankCardBindCaptcha(ApiRequestModel apiRequestModel) {
        try {
            BankCardBindCaptchaRequest bankCardBindCaptchaRequest = JSON.parseObject(apiRequestModel.getData(), BankCardBindCaptchaRequest.class);
            if (!ValidatorUtil.isMobile(bankCardBindCaptchaRequest.getReserveMobile())) {
                String phoneMessage = "预留手机号[" + bankCardBindCaptchaRequest.getReserveMobile() + "]格式错误";
                log.info(phoneMessage);
                throw new CashChannelException(phoneMessage);
            }
            /*根据uid获取用户数据*/
            ThirdIdModel thirdIdModel = thirdCommonLogic.findThirdIdModelByThirdOrderId(bankCardBindCaptchaRequest.getOrderNo(), apiRequestModel.getChannel());
            UserInfoModel userInfoModel = userLogic.findUserInfoModelByUid(thirdIdModel.getCUserUid());
            checkCardUserInfo(userInfoModel, bankCardBindCaptchaRequest);

            /*新卡存入卡包*/
            if (!bankCardLogic.checkCardExist(bankCardBindCaptchaRequest.getIdNo(), bankCardBindCaptchaRequest.getName(), bankCardBindCaptchaRequest.getCardNo(),
                    bankCardBindCaptchaRequest.getReserveMobile(), bankCardBindCaptchaRequest.getBankCode())) {
                ResponseResult res = protocolLogic.submitProtocolChannel(bankCardBindCaptchaRequest.getName(), bankCardBindCaptchaRequest.getIdNo(),
                        bankCardBindCaptchaRequest.getCardNo(), bankCardBindCaptchaRequest.getReserveMobile(),
                        bankCardBindCaptchaRequest.getVerifyCode(), apiRequestModel.getChannel()+Constant.ProConstant.SMS_UNIQUECODE);
                if (res != null && "000000".equals(res.getResponseCode())) {
                    bankCardLogic.addChannelBankCardToPack(thirdIdModel.getCUserUid(), apiRequestModel.getChannel(),
                            bankCardBindCaptchaRequest.getBankCode(), bankCardBindCaptchaRequest.getBankName(), bankCardBindCaptchaRequest.getCardNo(),
                            bankCardBindCaptchaRequest.getIdNo(), bankCardBindCaptchaRequest.getName(), bankCardBindCaptchaRequest.getReserveMobile());
                    JSONObject mqParams = new JSONObject();
                    mqParams.put("channel", apiRequestModel.getChannel());
                    mqParams.put("appId", thirdIdModel.getCThirdId());
                    mqParams.put("cardNo",bankCardBindCaptchaRequest.getCardNo());
                    mqParams.put("userMobile",bankCardBindCaptchaRequest.getReserveMobile());
                    mqParams.put("bankName",bankCardBindCaptchaRequest.getBankName());
                    mqParams.put("uid",thirdIdModel.getCUserUid());
                    protocolLogic.sendSubmitProtocol(mqParams);
                } else {
                    throw new CashChannelException("签约失败");
                }
            } else {
                bankCardLogic.setChannelDefaultCard(bankCardBindCaptchaRequest.getCardNo(), thirdIdModel.getCUserUid(), apiRequestModel.getChannel());
            }
            /*设置默认卡*/
            bankCardLogic.updateFundCard(thirdIdModel.getCWdId() ,bankCardBindCaptchaRequest.getName(), bankCardBindCaptchaRequest.getIdNo(),
                    bankCardBindCaptchaRequest.getCardNo(), bankCardBindCaptchaRequest.getBankCode(), bankCardBindCaptchaRequest.getReserveMobile());
            bankCardLogic.feedBackBindCard(bankCardBindCaptchaRequest.getOrderNo(), 1, bankCardBindCaptchaRequest.getBankCode(),
                    bankCardBindCaptchaRequest.getBankName(), bankCardBindCaptchaRequest.getCardNo(), bankCardBindCaptchaRequest.getReserveMobile(),
                    "", apiRequestModel.getChannel(), false);
            return new ApiResponseModel<>(true, 0, "", new BankCardBindCaptchaResponse(BankCardBindStatusEnum.SUCCESS.getStatus(), ""));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ApiResponseModel<>(false, -1, "绑卡失败", new BankCardBindCaptchaResponse(BankCardBindStatusEnum.FAILED.getStatus(), ""));
        }
    }

    private void checkCardUserInfo(UserInfoModel userInfoModel, BankCardBindCaptchaRequest bankCardBindRequest) throws CashChannelException {
        if (!bankCardBindRequest.getName().equals(userInfoModel.getcName()) || !bankCardBindRequest.getIdNo().equals(userInfoModel.getcIdno())) {
            throw new CashChannelException("姓名身份证与订单的信息不符");
        }
    }

}
